package io.github.wppli.domain.recommond.rservice;

import cc.jq1024.middleware.redisson.IRedissonService;
import io.github.wppli.domain.recommond.model.entity.UserPostsBehaviorsEntity;
import io.github.wppli.domain.recommond.repository.IUserPostsBehaviorRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author li--jiaqiang 2025−03−16
 */
@Service
@AllArgsConstructor
public class UserPostsBehaviorService implements IUserPostsBehaviorService {

    private final IUserPostsBehaviorRepository userPostsBehaviorRepository;
    private final IRedissonService redissonService;

    @Override
    public List<UserPostsBehaviorsEntity> queryByUserId(Long userId) {
        String key = "USER_POSTS_BEHAVIORS_" + userId;
        List<UserPostsBehaviorsEntity> userPostsBehaviorsEntities = redissonService.getList(key, UserPostsBehaviorsEntity.class);
        if (userPostsBehaviorsEntities == null || userPostsBehaviorsEntities.isEmpty()) {
            userPostsBehaviorsEntities = userPostsBehaviorRepository.queryByUserId(userId);
            redissonService.setValue(key, userPostsBehaviorsEntities, 60 + new Random().nextInt(20), TimeUnit.MINUTES);
        }
        return userPostsBehaviorsEntities;
    }
}